﻿Imports System.Data.SqlClient
Public Class Totalmoney
    Dim cmd As New SqlCommand

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Totalmoney_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Main.lblheader.Text = "ទូទាត់ប្រាក់"
        condb()
        grdinvoice.Rows.Clear()
        cmd.Connection = cn
        cmd.CommandText = "select InvoiceID,tableName from tblinvoice where Charge='NO'"
        Dim rd As SqlDataReader = cmd.ExecuteReader
        While rd.Read = True
            Me.grdinvoice.Rows.Add(rd(0), rd(1))
        End While
        rd.Close()
        ' Fore total Discont
        Try
            'txttotalmoney.Text = sallfood.txttotal.Text
            Dim a, b, c, total As Double
            a = Val(txttotalmoney.Text)
            b = Val(txtdiscon.Text)
            c = a * b \ 100
            total = a - c
            txtafterdiscon.Text = total
        Catch ex As Exception
        End Try

    End Sub


    Private Sub grdinvoice_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdinvoice.SelectionChanged
        If grdinvoice.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row As DataGridViewRow = Me.grdinvoice.SelectedRows(0)
        Me.txtinvoice.Text = row.Cells("invoiceNo").Value
    End Sub

    Private Sub txtafterdiscon_MouseHover1(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtafterdiscon.MouseHover
            Dim a, b, c, total As Double
            a = Val(txttotalmoney.Text)
            b = Val(txtdiscon.Text)
            c = a * b \ 100
            total = a - c
        txtafterdiscon.Text = total.ToString
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        cmd.Connection = cn
        cmd.CommandText = "INSERT INTO TotalAmount VALUES (N'" & txtinvoice.Text & "',N'" & txtdiscon.Text & "',N'" & txtafterdiscon.Text & "')"
        cmd.ExecuteNonQuery()
        txtsuccess.Text = "Successfull..."

        'for Delete Invoice
        cmd.Connection = cn
        cmd.CommandText = "update tblinvoice set charge = 'yes' where invoiceID='" & txtinvoice.Text & "'"
        cmd.ExecuteNonQuery()

        Dim frn As New InvoiceReport
        frn.ShowDialog()
        If grdinvoice.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Me.grdinvoice.Rows.Remove(grdinvoice.CurrentRow)

    End Sub

    Private Sub txtinvoice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtinvoice.TextChanged
        condb()
        Dim cmd As New SqlCommand("select Totalpay from tblinvoice where invoiceID='" & txtinvoice.Text & "'", cn)
        txttotalmoney.Text = cmd.ExecuteScalar


    End Sub

    Private Sub txtdiscon_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtdiscon.TextChanged
        Dim a, b, c, total As Double
        a = Val(txttotalmoney.Text)
        b = Val(txtdiscon.Text)
        c = a * b \ 100
        total = a - c
        txtafterdiscon.Text = total
    End Sub

    Private Sub txtgetmoney_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtgetmoney.TextChanged
        Try
            Dim totalamount As Double
            If txtgetmoney.Text > 0 Then
                totalamount = Val(txtgetmoney.Text) - Val(txtafterdiscon.Text)
                txttotalamount.Text = totalamount
            Else
                txttotalamount.Text = 0
            End If
            If CDbl(txtgetmoney.Text) < CDbl(txtafterdiscon.Text) Then
                txttotalamount.Text = "លុយមិនគ្រប់ទេ!!"
            Else
                txttotalamount.Text = totalamount
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        changmoney.ShowDialog()
    End Sub

    Private Sub txttotalmoney_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttotalmoney.TextChanged
        txtafterdiscon.Text = txttotalmoney.Text
    End Sub

    Private Sub txtafterdiscon_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtafterdiscon.TextChanged
        condb()
        cmd.Connection = cn
        cmd.CommandText = "select Rial from changmoney"
        Dim rial = cmd.ExecuteScalar
        txttotalrial.Text = Val(txtafterdiscon.Text) * Val(rial)


    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim i As Integer
        i += 1
        If i = 1 Then
            lblthank.ForeColor = Color.Red
        ElseIf i = 2 Then
            lblthank.ForeColor = Color.Blue
        Else
            i = 0
            lblthank.ForeColor = Color.Aqua
        End If
    End Sub
End Class